from common.logger_helper import g_wlogger
from common.utils import get_lastyear_mouth
from k3_data_report.business.cgtj_db import Cgtj

from k3_data_report.business.chtj_process import baifenbi_data_kh, compare_data_kh
from k3_data_report.business.config import bussiness_cal_fields
from k3_data_report.business.xstj_db import Xstj

__author__ = "Ypf"
_date_ = "2019/7/23-10:31"


def compare_data_pp(dic1, dic2):
    for key_1 in dic1.keys():
        if key_1 in dic2.keys():
            dic1[key_1].append(dic2[key_1][2])
        else:
            dic1[key_1].append(0)
    for key_2 in dic2.keys():
        if key_2 not in dic1.keys():
            dic2[key_2].append(dic2[key_2][2])
            dic2[key_2][2] = 0
            dic1[key_2] = dic2[key_2]
    return dic1


def xstj_pp(time1):
    time2 = get_lastyear_mouth(time1)
    db_sql = bussiness_cal_fields['k3_caozuo']
    # 按照客户品牌分类
    check_sql = db_sql["check_sql"] % ('k3xstjcp', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        sql1_pp = db_sql["xsdd_ddtj_pp"] % (time1)
        sql2_pp = db_sql["xsdd_ddtj_pp"] % (time2)
        sql3_pp = db_sql["insert_ddtj_pp"]
        xstj_dic1_pp = Xstj.get_dic_pp(sql1_pp)
        xstj_dic2_pp = Xstj.get_dic_pp(sql2_pp)
        xstj_dic_pp = compare_data_pp(xstj_dic1_pp, xstj_dic2_pp)
        xstj_pp = baifenbi_data_kh(xstj_dic_pp)
        Xstj.insert_pp(sql3_pp, xstj_pp, time1)
    else:
        g_wlogger.winfo(f"xstj_pp-{time1}:target_full_df, all data is empty.")

def baifenbi_data_xs(dic):
    for key in dic:
        a = dic[key][2]
        b = dic[key][3]
        if b == 0:
            dic[key].append(0)
        else:
            c = (a - b) / b
            dic[key].append(c)
    return dic
def xstj_kh(time1):
    time2 = get_lastyear_mouth(time1)
    db_sql=bussiness_cal_fields['k3_caozuo']
    # 按照客户分类
    check_sql = db_sql["check_sql"] % ('k3xstjkh', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        sql1_kh = db_sql["xsdd_ddtj_kh"] % (time1)
        sql2_kh = db_sql["xsdd_ddtj_kh"] % (time2)
        sql3_kh = db_sql["insert_ddtj_kh"]
        xstj_dic1_kh = Xstj.get_dic_kh(sql1_kh)
        xstj_dic2_kh = Xstj.get_dic_kh(sql2_kh)
        xstj_dic_kh = compare_data_kh(xstj_dic1_kh, xstj_dic2_kh)
        xstj_kh=baifenbi_data_kh(xstj_dic_kh)
        Xstj.insert_kh(sql3_kh, xstj_kh, time1)
    else:
        g_wlogger.winfo(f"xstj_kh-{time1}:target_full_df, all data is empty.")

    # 按照客户经理品牌分类
    check_sql = db_sql["check_sql"] % ('k3xstjxs', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        sql1_xs = db_sql["xsdd_ddtj_xs"] % (time1)
        sql2_xs = db_sql["xsdd_ddtj_xs"] % (time2)
        sql3_xs = db_sql["insert_ddtj_xs"]
        xstj_dic1_xs = Xstj.get_dic_jl(sql1_xs)
        xstj_dic2_xs = Xstj.get_dic_jl(sql2_xs)
        xstj_dic_xs = compare_data_pp(xstj_dic1_xs, xstj_dic2_xs)
        xstj_xs=baifenbi_data_xs(xstj_dic_xs)
        Xstj.insert_xs(sql3_xs, xstj_xs, time1)
    else:
        g_wlogger.winfo(f"xstj_xs-{time1}:target_full_df, all data is empty.")

def xstj_pp_main(month):
    try:
        xstj_pp(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"xstj_pp_main has error:{e} with:{month}")
        return False
def xstj_kh_main(month):
    try:
        xstj_kh(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"xstj_kh_main has error:{e} with:{month}")
        return False
if __name__ == "__main__":
    time1='2019-07'
    time2='2018-07'
    xstj_pp("2019-01")
    xstj_kh("2019-02")